/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.approve.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.approve.entity.HomeApprove;
import com.fosung.frame.modules.approve.entity.PartyStatistic;
import com.fosung.frame.modules.approve.entity.PersonStatistic;
import com.fosung.frame.modules.approve.service.DzbStatisticService;
import com.fosung.frame.modules.approve.service.HomeApproveService;
import com.fosung.frame.modules.life.entity.OrganizeLife;
import com.fosung.frame.modules.life.service.OrganizeLifeService;
import com.fosung.frame.modules.life.service.OrganizeLifeTypeService;
import com.fosung.frame.modules.sys.entity.Dict;
import com.fosung.frame.modules.sys.utils.DictUtils;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 首页Controller
 * 
 * @author ygl
 * @version 2017-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/approve/homeDzb")
public class HomeDzbController extends BaseController {

	@Autowired
	private HomeApproveService homeApproveService;

	@Autowired
	private OrganizeLifeService organizeLifeService;

	@Autowired
	private OrganizeLifeTypeService organizeLifeTypeService;

	@Autowired
	private DzbStatisticService dzbStatisticService;

	@ModelAttribute
	public HomeApprove get(@RequestParam(required = false) String id) {
		HomeApprove entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = homeApproveService.get(id);
		}
		if (entity == null) {
			entity = new HomeApprove();
		}
		return entity;
	}

	@RequiresPermissions("approve:homeDzb:view")
	@RequestMapping(value = { "list", "" })
	public String list(HomeApprove homeApprove, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Dict> findAssessmentTimeList = DictUtils.findAssessmentTimeList();
		String year = findAssessmentTimeList.size() > 0 ? findAssessmentTimeList.get(0).getLabel()
				: new SimpleDateFormat("yyyy").format(new Date());

		PartyStatistic partyStatistic = new PartyStatistic();
		partyStatistic.setYear(year);
		partyStatistic.setOrganizationId(UserUtils.getUser().getOffice().getId());
		partyStatistic = dzbStatisticService.getScorebyOrgid(partyStatistic);

		model.addAttribute("year", year);
		model.addAttribute("partyStatistic", partyStatistic);
		return "modules/approve/homeDzbList";
	}

	/**
	 * 查询组织生活申请待审核数据
	 * 
	 * @param homeApprove
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findOrganizeLifeJhList")
	@ResponseBody
	public List<OrganizeLife> findOrganizeLifeJhList(OrganizeLife organizeLife, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		organizeLife.setColumn25("0");
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
		List<OrganizeLife> olList = organizeLifeService.findList(organizeLife);
		// 因为组织生活分类可能包括多个，这里先spilt再单独查询拼成字符串显示到前台list
		if (olList.size() > 0) {
			for (int i = 0; i < olList.size(); i++) {
				StringBuffer typeNames = new StringBuffer();
				OrganizeLife o = olList.get(i);
				if (o.getTypeids() != null && !o.getTypeids().equals("")) {
					String[] types = o.getTypeids().split(",");
					if (types != null && types.length > 0) {
						for (String s : types) {
							// 循环遍历数组中元素，添加到StringBuilder 对象中
							String t = organizeLifeTypeService.get(s).getTypename();
							typeNames.append(t + ",");
						}
					}
					if (typeNames.length() > 0) {
						// 调用字符串的deleteCharAt()方法,删除最后一个多余的逗号
						typeNames.deleteCharAt(typeNames.length() - 1);
					}
				}
				o.setTypeids(typeNames.toString());
			}
		}
		return olList;
	}

	/**
	 * 首页功能 查询积分申请数据
	 * 
	 * @param homeApprove
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findOrganizeLifeJfList")
	@ResponseBody
	public List<HomeApprove> findOrganizeLifeJfList(HomeApprove homeApprove, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<HomeApprove> list = homeApproveService.findOrganizeLifeJfList(homeApprove);
		return list;
	}

	/**
	 * 查询党员排名
	 * 
	 * @param personStatistic
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findPersonPmList")
	@ResponseBody
	public List<PersonStatistic> findPersonPmList(PersonStatistic personStatistic, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		personStatistic.setOrganizationId(UserUtils.getUser().getOffice().getId());
		personStatistic.setNum(10);
		List<PersonStatistic> list = dzbStatisticService.findPersonPmList(personStatistic);
		return list;
	}

	/**
	 * 查询党组织排名 前10名
	 * 
	 * @param partyStatistic
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findPartyPmList")
	@ResponseBody
	public List<PartyStatistic> findPartyPmList(PartyStatistic partyStatistic, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String organizationId = UserUtils.getUser().getOffice().getId();
		partyStatistic.setOrganizationId(organizationId);
		List<PartyStatistic> list = dzbStatisticService.findPartyPmList(partyStatistic);
		return list;
	}

}